﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureBeamBreakBehavior.h"
#include "GbmpBuiltInParameterDefinitions.h"
#include "IDocument.h"
#include "IElement.h"
#include "IElementParameters.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

void gcmp::StructureBeamBreakBehavior::BreakConcreteElement(const IDocument * pDoc, IElement * pBreakElement, IElement * pNewElement, double breakPtToStartPtRatio, bool isBreakElementRetainOldElementId)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc != nullptr, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pBreakElement != nullptr, L"pBreakElement为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(pNewElement != nullptr, L"pNewElement为空",L"GDMPLab",L"2024-03-30");

    if (!pBreakElement->GetElementParameters() || !pBreakElement->GetElementParameters()->HasParameterByUid(PARAMETER_UID(StartOffsetBuiltInParameter)))
    {
        return;
    }

    double startPointOffset = pBreakElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(StartOffsetBuiltInParameter))->GetValueAsDouble();  //起点标高偏移
    double endPointOffset = pBreakElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(EndOffsetBuiltInParameter))->GetValueAsDouble();    //

    double diffOffset = fabs(endPointOffset - startPointOffset);
    double breakPointOffset = 0;
    if (MathUtils::IsEqual(startPointOffset, endPointOffset, Constants::LENGTH_EPS))
    {
        return;
    }
    else if (MathUtils::IsLessThan(startPointOffset, endPointOffset, Constants::LENGTH_EPS))
    {
        breakPointOffset = breakPtToStartPtRatio*diffOffset;
    }
    else if (MathUtils::IsGreaterThan(startPointOffset, endPointOffset, Constants::LENGTH_EPS))
    {
        breakPointOffset = (1 - breakPtToStartPtRatio)*diffOffset;
    }

    breakPointOffset += min(startPointOffset, endPointOffset);

    if (!pBreakElement->GetElementParameters() || !pNewElement->GetElementParameters())
        return;
    if (isBreakElementRetainOldElementId)
    {
        OwnerPtr<IParameter> endOffsetParam = pBreakElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(EndOffsetBuiltInParameter));
        OwnerPtr<IParameter> startOffsetParam = pNewElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(StartOffsetBuiltInParameter));
        endOffsetParam->SetValueAsDouble(breakPointOffset);
        startOffsetParam->SetValueAsDouble(breakPointOffset);
        pBreakElement->GetElementParameters()->SetParameter(endOffsetParam.get());
        pNewElement->GetElementParameters()->SetParameter(startOffsetParam.get());
    }
    else
    {
        OwnerPtr<IParameter> endOffsetParam = pNewElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(EndOffsetBuiltInParameter));
        OwnerPtr<IParameter> startOffsetParam = pBreakElement->GetElementParameters()->GetParameterByUid(PARAMETER_UID(StartOffsetBuiltInParameter));
        endOffsetParam->SetValueAsDouble(breakPointOffset);
        startOffsetParam->SetValueAsDouble(breakPointOffset);
        pNewElement->GetElementParameters()->SetParameter(endOffsetParam.get());
        pBreakElement->GetElementParameters()->SetParameter(startOffsetParam.get());
    }
}
